﻿using System;
using System.Diagnostics;

namespace Bing
{
    /// <summary>
    /// Contains parameters for a request to the Web <see cref="ApiHelper.SourceType"/>.
    /// </summary>
    [DebuggerDisplay("Count={Count}, FileType: {FileType}")]
    public class WebRequest: IBingRequest
    {

        /// <summary>
        /// Gets or sets specifies the number of results to return per <see cref="WebRequest"/>.
        /// </summary>
        /// <value>Specifies the number of results to return per <see cref="WebRequest"/>.</value>
        /// <remarks>
        /// If the number of results per page requested in the WebRequest.Count field is greater than the total number of results returned in the WebResponse.Total field, the number of results per page will be equal to the total number of results.<br />
        /// The minimum value for Count is 1; the maximum value is 50. If a value outside this range is specified, the Bing API returns an error.<br />
        /// Search results are limited to the first 1,000 results. This means that the total of WebRequest.Count plus WebRequest.Offset should not be greater than 1,000.<br />
        /// </remarks>
        public UInt32 Count { get; set; }

        /// <summary>
        /// Gets or sets the specifies the offset requested, from zero, for the starting point of the result set to be returned for this <see cref="WebRequest"/>.
        /// </summary>
        /// <value>Specifies the offset requested, from zero, for the starting point of the result set to be returned for this <see cref="WebRequest"/>.</value>
        /// <remarks>
        /// The offset specifies the requested position in the result set for the response that is returned for this source. <br />
        /// <b>Offset</b> is typically used in conjunction with the <b>Count</b> property on the <b>SourceRequest</b> object, and the <b>Total</b> property on the <see cref="WebResponse"/> object, to facilitate paging through long lists of results.<br />
        /// The default value for <b>>Offset</b> is 0 (zero), and the maximum offset value is 1,000. If any other value is specified, an error is returned.<br />
        /// Search results are limited to the first 1,000 results. This means that the total of <b><see cref="WebRequest"/>.Count</b> plus <b><see cref="WebRequest"/>.Offset</b> should not be greater than 1,000.<br />
        /// </remarks>
        public UInt32 Offset { get; set; }

        /// <summary>
        /// Gets or sets the type of files (for example, Microsoft Office Word .DOC files or Adobe Acrobat .PDF files) to return from the Web <see cref="ApiHelper.SourceType"/>.
        /// </summary>
        /// <value>Specifies the type of files (for example, Microsoft Office Word .DOC files or Adobe Acrobat .PDF files) to return from the WebSourceType.</value>
        /// <remarks>
        /// <p>For the <strong>Web</strong><see cref="ApiHelper.SourceType"/>, the <strong>FileType</strong> property specifies the file extension. The following file extensions are supported:</p>
        /// <h3 class="subHeading"></h3>
        /// <table>
        ///   <tbody><tr>
        ///     <th colspan="1">
        ///       <strong>Web File Type</strong>
        ///     </th>
        ///     <th colspan="1">
        ///       <strong>FileType Property Value</strong>
        ///     </th>
        ///   </tr>
        ///   <tr>
        ///     <td colspan="1">
        ///       <p>Microsoft Word Document</p>
        ///     </td>
        ///     <td colspan="1">
        ///       <p>DOC</p>
        ///     </td>
        ///   </tr>
        ///   <tr>
        ///     <td colspan="1">
        ///       <p>Autodesk Drawing File</p>
        ///     </td>
        ///     <td colspan="1">
        ///       <p>DWF</p>
        ///     </td>
        ///   </tr>
        ///   <tr>
        ///     <td>
        ///       <p>Really Simple Syndication (RSS) Feed</p>
        ///     </td>
        ///     <td>
        ///       <p>FEED</p>
        ///     </td>
        ///   </tr>
        ///   <tr>
        ///     <td colspan="1">
        ///       <p>Hypertext Markup Language (.htm) File</p>
        ///     </td>
        ///     <td colspan="1">
        ///       <p>HTM</p>
        ///     </td>
        ///   </tr>
        ///   <tr>
        ///     <td colspan="1">
        ///       <p>Hypertext Markup Language (.html) File</p>
        ///     </td>
        ///     <td colspan="1">
        ///       <p>HTML</p>
        ///     </td>
        ///   </tr>
        ///   <tr>
        ///     <td colspan="1">
        ///       <p>Adobe Acrobat Portable Document</p>
        ///     </td>
        ///     <td colspan="1">
        ///       <p>PDF</p>
        ///     </td>
        ///   </tr>
        ///   <tr>
        ///     <td colspan="1">
        ///       <p>Microsoft PowerPoint Presentation</p>
        ///     </td>
        ///     <td colspan="1">
        ///       <p>PPT</p>
        ///     </td>
        ///   </tr>
        ///   <tr>
        ///     <td colspan="1">
        ///       <p>Adobe PostScript Document</p>
        ///     </td>
        ///     <td colspan="1">
        ///       <p>PS</p>
        ///     </td>
        ///   </tr>
        ///   <tr>
        ///     <td colspan="1">
        ///       <p>Microsoft Rich Text Format Document</p>
        ///     </td>
        ///     <td colspan="1">
        ///       <p>RTF</p>
        ///     </td>
        ///   </tr>
        ///   <tr>
        ///     <td colspan="1">
        ///       <p>Generic Text (.text) File</p>
        ///     </td>
        ///     <td colspan="1">
        ///       <p>TEXT</p>
        ///     </td>
        ///   </tr>
        ///   <tr>
        ///     <td colspan="1">
        ///       <p>Generic Text (.txt) File</p>
        ///     </td>
        ///     <td colspan="1">
        ///       <p>TXT</p>
        ///     </td>
        ///   </tr>
        ///   <tr>
        ///     <td colspan="1">
        ///       <p>Microsoft Excel Workbook</p>
        ///     </td>
        ///     <td colspan="1">
        ///       <p>XLS</p>
        ///     </td>
        ///   </tr>
        /// </tbody></table>
        /// </remarks>
        public string FileType { get; set; }

        /// <summary>
        /// Gets or sets the options for this <see cref="WebRequest"/>.
        /// </summary>
        /// <value>TSpecifies options for this <see cref="WebRequest"/>.</value>
        public ApiHelper.WebSearchOption[] Options { get; set; }

        // TODO: Documentation Error: http://msdn.microsoft.com/en-us/library/dd877878.aspx, No introduction text

        /// <summary>
        /// Gets or sets the search tags.
        /// </summary>
        /// <value>The search tags.</value>
        public WebSearchTag[] SearchTags { get; set; }

        /// <summary>
        /// Creates the query string for the API call.
        /// </summary>
        /// <returns>The query string to append to the call.</returns>
        public string ToQueryString()
        {
            string request = string.Format("&Web.Count={0}", Count);
            request += string.Format("&Web.Offset={0}", Offset);
            if (FileType != null) request += string.Format("&Web.FileType={0}", FileType);

            if (Options != null)
            {
                foreach (ApiHelper.WebSearchOption option in Options)
                {
                    request += string.Format("&Web.Options={0}",
                                             Enum.GetName(typeof (ApiHelper.WebSearchOption), option));
                }
            }

            if (SearchTags != null)
            {
                foreach (WebSearchTag searchTag in SearchTags)
                {
                    request += string.Format("&Web.SearchTag.Name={0}&Web.SearchTag.Value={1}", searchTag.Name,
                                             searchTag.Value);
                }
            }
            return request;
        }
    }
}
